﻿#define _CAMERABOX_TRACKBAR_ENABLE

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

using Declarations;
using Declarations.Events;
using Declarations.Media;
using Declarations.Players;
using Implementation;

namespace SDK10kWrapper
{
    partial class CameraBox
    {

        #region VLC Event - BACKUP only
        void Events_PlayerStopped(object sender, EventArgs e)
        {
            //UISync.Execute(() => InitControls());
        }

        void Events_MediaEnded(object sender, EventArgs e)
        {
            //UISync.Execute(() => InitControls());
        }

        private void InitControls()
        {
            //trackBar1.Value = 0;
            //lblTime.Text = "00:00:00";
            //lblDuration.Text = "00:00:00";
        }

        void Events_StateChanged(object sender, MediaStateChange e)
        {
            //UISync.Execute(() => label1.Text = e.NewState.ToString());
        }

        void Events_DurationChanged(object sender, MediaDurationChange e)
        {
            //UISync.Execute(() => lblDuration.Text = TimeSpan.FromMilliseconds(e.NewDuration).ToString().Substring(0, 8));
        }

        void Events_ParsedChanged(object sender, MediaParseChange e)
        {
            Console.WriteLine(e.Parsed);
        }
        #endregion

        #region Event for Trackbar
        void Events_TimeChanged(object sender, MediaPlayerTimeChanged e)
        {
#if _CAMERABOX_TRACKBAR_ENABLE
            UISync.Execute(() =>
            {
                if (justPlay)
                {
                    lock (locker) // Chi tung thang duoc phep thay doi cai nay ma thoi
                    {   // Resource use in this lock (duration : only update here | cList : also use lock already)
                        this.duration = m_player.Length; // Do we really need duration khong nhi ? or use direct m_player.Length

                        MaxDuration = this.duration; MaxCamera = this;

                        foreach (CameraBox cam in cList)
                        {
                            if (cam == null) continue; if (cam.Disposing) continue; if (cam.IsDisposed) continue;
                            if (cam.ParentForm != this.ParentForm) continue; if (cam == this) continue;
                            if (cam.duration > MaxDuration) { MaxDuration = cam.duration; MaxCamera = cam; }
                        }
                    }
                    justPlay = false;
                }

                if (this != MaxCamera) return; // Check if Maximum Duration Camera then Bind to Trackbar
                if (_Offline_SlideBarForAll == null) return;
                int val = Convert.ToInt32(m_player.Time * _Offline_SlideBarForAll.Maximum / MaxDuration);
                _Offline_SlideBarForAll.Value = val > _Offline_SlideBarForAll.Maximum ? _Offline_SlideBarForAll.Maximum : val;
            });
#endif
        }

        void Events_PlayerPositionChanged(object sender, MediaPlayerPositionChanged e)
        {   // Implement INotifyChange Interface for Offline_VideoPlayPosition (2-ways binding)
#if _CAMERABOX_TRACKBAR_ENABLE
            UISync.Execute(() => { _Offline_VideoPlayPosition = e.NewPosition; OnPropertyChanged("Offline_VideoPlayPosition"); });
#endif
        }

        class UISync
        {
            static ISynchronizeInvoke _sync;
            public static void Init(ISynchronizeInvoke sync)
            {
                _sync = sync;
            }

            public static void Execute(Action action)
            {
                if (_sync.InvokeRequired) _sync.BeginInvoke(action, null);
                else action();
            }

        }
        #endregion
        

    }
}
